package com.example.barcodeapp.ui.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.barcodeapp.MainActivity;
import com.example.barcodeapp.R;
import com.example.barcodeapp.adapter.gatheradapter.AdapterJiangShi;
import com.example.barcodeapp.adapter.gatheradapter.AdapterMianFei;
import com.example.barcodeapp.adapter.gatheradapter.AdapterZhiBo;
import com.example.barcodeapp.base.BaseFragment;
import com.example.barcodeapp.common.Constants;
import com.example.barcodeapp.interfaces.IBasePersenter;
import com.example.barcodeapp.ui.home.activity.IssueActivity;
import com.example.barcodeapp.ui.home.activity.JiangshiActivity;
import com.example.barcodeapp.ui.home.activity.MianFeiActivity;
import com.example.barcodeapp.ui.home.activity.XiaoXiActivity;
import com.example.barcodeapp.ui.home.activity.YuYueActivity;
import com.example.barcodeapp.ui.home.bean.JiaoShiLeiBiaoBean;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.internal.http2.Http2;

public class GatherFragment extends BaseFragment {


    @BindView(R.id.tishi_lingdang)
    ImageView tishiLingdang;
    @BindView(R.id.tishi_hongdian)
    ImageView tishiHongdian;
    @BindView(R.id.toobar_loge_news)
    RelativeLayout toobarLogeNews;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.peixun)
    ImageView peixun;
    @BindView(R.id.wenti)
    ImageView wenti;
    @BindView(R.id.peixunwenti)
    RelativeLayout peixunwenti;
    @BindView(R.id.onego)
    ImageView onego;
    @BindView(R.id.jieshaowenzi)
    TextView jieshaowenzi;
    @BindView(R.id.jieshao_gengduo)
    TextView jieshaoGengduo;
    @BindView(R.id.jieshaoneirong)
    RecyclerView jieshaoneirong;
    @BindView(R.id.constraintLayout)
    ConstraintLayout constraintLayout;
    @BindView(R.id.zhibo_wenzi)
    TextView zhiboWenzi;
    @BindView(R.id.zhibo_neirong)
    RecyclerView zhiboNeirong;
    @BindView(R.id.zhibo)
    ConstraintLayout zhibo;
    @BindView(R.id.mianfei_wenzi)
    TextView mianfeiWenzi;
    @BindView(R.id.mianfei_neirong)
    RecyclerView mianfeiNeirong;
    Context mContext;
    private List<JiaoShiLeiBiaoBean.DataEntity.TeacherEntity> teacherEntities;
    private List<JiaoShiLeiBiaoBean.DataEntity.KechengEntity> kechengEntities;
    private List<JiaoShiLeiBiaoBean.DataEntity.ZhiboEntity> zhiboEntities;
    private JiaoShiLeiBiaoBean jiaoShiLeiBiaoBean;
    private AdapterJiangShi adapterJiangShi;

    private AdapterZhiBo adapterZhiBo;
    private AdapterMianFei adapterMianFei;

    public GatherFragment(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_gather;
    }

    @Override
    protected void initView() {
        teacherEntities = new ArrayList<>();
        kechengEntities = new ArrayList<>();
        zhiboEntities = new ArrayList<>();
        jiaoShiLeiBiaoBean = new JiaoShiLeiBiaoBean();
        Http3();
        jieshaoGengduo.setOnClickListener(new View.OnClickListener() {
                                              @Override
                                              public void onClick(View view) {
                                                  startActivity(new Intent(getActivity(), JiangshiActivity.class));
                                              }
                                          }
        );

        wenti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), IssueActivity.class));
            }
        });
        onego.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), YuYueActivity.class));

            }
        });
        peixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(getContext(), MainActivity.class);
//                startActivityForResult(intent, 1);
                ((MainActivity) getActivity()).zhuan();
            }
        });
        tishiLingdang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),  XiaoXiActivity.class));
            }
        });

    }

    private void Http3() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://new.jifengyinyue.com/api/index/index");
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    int responseCode = urlConnection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        InputStream inputStream = urlConnection.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        String line = null;
                        StringBuffer stringBuffer = new StringBuffer();
                        while ((line = bufferedReader.readLine()) != null) {
                            stringBuffer.append(line);
                        }
                        String json = stringBuffer.toString();
                        final Gson gson = new Gson();
                        final JiaoShiLeiBiaoBean fengzhuanglei = gson.fromJson(json, JiaoShiLeiBiaoBean.class);
                        getActivity().runOnUiThread(new Runnable() {

                            private ArrayList<JiaoShiLeiBiaoBean> fenghzuangleis;

                            @Override
                            public void run() {
                                jiaoShiLeiBiaoBean = fengzhuanglei;
                                teacherEntities = fengzhuanglei.getData().getTeacher();
                                Log.e("asdawd", "run: " + fengzhuanglei.toString());
                                kechengEntities = fengzhuanglei.getData().getKecheng();
                                zhiboEntities = fengzhuanglei.getData().getZhibo();
                                fenghzuangleis = new ArrayList<>();
                                ArrayList<String> list = new ArrayList<>();
                                for (int i = 0; i < fengzhuanglei.getData().getLunbo().size(); i++) {
                                    list.add(fengzhuanglei.getData().getLunbo().get(i));
                                }

                                banner.setAdapter(new BannerImageAdapter<String>(list) {
                                    @Override
                                    public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                                        //图片加载自己实现
                                        Glide.with(holder.itemView)
                                                .load(Constants.Base_TPUrl2 + data)
                                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                                .into(holder.imageView);
                                    }
                                })
                                        .addBannerLifecycleObserver(getActivity())//添加生命周期观察者
                                        .setIndicator(new CircleIndicator(context));
                                adapterJiangShi = new AdapterJiangShi(getActivity(), teacherEntities);
                                LinearLayoutManager manager = new LinearLayoutManager(context);
                                manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                                jieshaoneirong.setLayoutManager(manager);
                                jieshaoneirong.setAdapter(adapterJiangShi);
                                //直播
                                adapterZhiBo = new AdapterZhiBo(getActivity(), zhiboEntities);
                                LinearLayoutManager manager2 = new LinearLayoutManager(context);
                                manager2.setOrientation(LinearLayoutManager.HORIZONTAL);
                                zhiboNeirong.setLayoutManager(manager2);
                                zhiboNeirong.setAdapter(adapterZhiBo);
                                //免费课程 mianfeiNeirong
                                adapterMianFei = new AdapterMianFei(getActivity(), kechengEntities);
                                LinearLayoutManager manager3 = new LinearLayoutManager(context);
                                manager3.setOrientation(LinearLayoutManager.HORIZONTAL);
                                mianfeiNeirong.setLayoutManager(manager3);
                                mianfeiNeirong.setAdapter(adapterMianFei);
                                adapterJiangShi.notifyDataSetChanged();

                                adapterJiangShi.setClick(new AdapterJiangShi.IClick() {
                                    @Override
                                    public void click(int pos) {
                                        startActivity(new Intent(getActivity(), JiangshiActivity.class));
                                    }
                                });
                                adapterMianFei.setCallback(new AdapterMianFei.IClick() {
                                    @Override
                                    public void click(int pos) {
                                        Constants.mianfeikecheng=kechengEntities.get(pos).getId();
                                        startActivity(new Intent(getActivity(), MianFeiActivity.class));
                                    }
                                });





                            }
                        });

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    @Override
    protected IBasePersenter initPersenter() {
        return null;
    }

    @Override
    protected void initData() {

    }


}